<?


	include('config.php');
	$link = mysql_connect($CONFIG['dbserver'], $CONFIG['dbuser'], $CONFIG['dbpass']) or die("cant connect to db".mysql_error());


function getDbUsers(){
/* index.php uses this to list the database users in option boxes */
	global $CONFIG;
// connect to the database
	$link = mysql_connect($CONFIG['dbserver'], $CONFIG['dbuser'], $CONFIG['dbpass']);
	
// Check the database connection
	
	if (!$link) {
		           die('Not connected : ' . mysql_error());
	}
// Select the database and check that it was selected properly
	$dbselected = mysql_select_db('mysql') or die("Couldn't select database");
	if (!$dbselected) {
		           die ('Can\'t use mysql : ' . mysql_error());
	}

// Query the database and get a list of users and check to make sure everything went ok
	$dbquery = "SELECT User FROM user";
	$result=mysql_query($dbquery) or die(mysql_error());

	if (!link) {
		       $message  = 'Invalid query: ' . mysql_error() . "\n";
			   $message .= 'Whole query: ' . $query;
			   die($message);
	}
// Print the users to option boxes
	$retVar="";
	while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		if ($row['User'] != "root"){
			$retVar .= "<option>{$row['User']}</option>";
		}	}
		return $retVar;
	
	
}

function listDbUsers(){
/* index.php uses this to list the database users in option boxes */
	global $CONFIG;
// connect to the database
	$link = mysql_connect($CONFIG['dbserver'], $CONFIG['dbuser'], $CONFIG['dbpass']);
// Check the database connection
	
	if (!$link) {
		           die('Not connected : ' . mysql_error());
	}
// Select the database and check that it was selected properly
	$dbselected = mysql_select_db('mysql') or die("Couldn't select database");
	if (!$dbselected) {
		           die ('Can\'t use mysql : ' . mysql_error());
	}

// Query the database and get a list of users and check to make sure everything went ok
	$dbquery = "SELECT User FROM mysql.user";
	$result=mysql_query($dbquery);

	if (!link) {
		       $message  = 'Invalid query: ' . mysql_error() . "\n";
			   $message .= 'Whole query: ' . $query;
			   die($message);
	}
// Print the users to option boxes
	$retVar="";
	while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		if ($row['User'] != "root"){
		$retVar .= "{$row['User']} <a href=\"http://localhost/koodb/deluser.php?delUser={$row['User']} \">Delete user</a><br />";
		}
	}
		return $retVar;
}

function getDbs(){
// index.php uses this to list the databases in option boxes

	global $CONFIG;
// connect to the database
	$link = mysql_connect($CONFIG['dbserver'], $CONFIG['dbuser'], $CONFIG['dbpass']);

	//get a list of dbs in an array
			$db_list = mysql_list_dbs($link);
		//process the array
			$retVar = "";
			while ($row = mysql_fetch_object($db_list)) {
				$dbArray[]=$row->Database;
			}
		//print the databases in option boxes
			foreach($dbArray as $db){
				if ($db!="information_schema") { 
				$retVar.="<option>{$db}</option>";
				}
			}
		return $retVar;	
}

function showDbs(){
	
	    $showDbQuery="SHOW DATABASES";
        $result=mysql_query($showDbQuery);


	if (!$result) {
	           $message  = 'Invalid query: ' . mysql_error() . "\n";
			   $message .= 'Whole query: ' . $query;
			   die($message);
	}	
		$retVar="";
		$placeholder="20";
				echo "\n<table>\n";
	while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		if($row['Database']!="information_schema"){
		
		$currentDb = $row['Database'];

		echo "<tr><td>{$row['Database']}    <a href=\"http://localhost/koodb/deldb.php?delDb={$row['Database']}\">Delete database</a></td><br />\n";
		showUserDbPerms($currentDb);
		echo "<br />";
		}
		
		echo "</table>\n";
	}
		//return $retVar;
		

}

function showUserDbPerms($currentDb){

	mysql_select_db('mysql');

	$getPermsQuery="SELECT host, user, Select_priv, Insert_priv FROM db WHERE db=\"".$currentDb."\"";
	$permQuery=mysql_query($getPermsQuery) or die('permquery fucked up'. mysql_error());
	$numresults = mysql_num_rows($permQuery);
//	echo "Got {$numresults} results.<br>\n";

		echo "<td>Users with access to $currentDb:<br></td>\n";
	

	
	while ($permrow = mysql_fetch_array($permQuery,MYSQL_ASSOC))
	{
		
		//echo "<div style=\"position: absolute; right: 0px; top: ".$placeholder."px;\">";
		echo "<td>";
		echo "{$permrow['user']} from {$permrow['host']} has ";
		if($permrow['Select_priv'] == 'Y')
			echo "SELECT, ";
		if($permrow['Insert_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Alter_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Update_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Delete_priv'] == 'Y')
			echo "INSERT, ";		
		if($permrow['Create_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Drop_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Reload_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Shutdown_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Process_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['File_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Grant_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['References_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Index_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Show_db_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Super_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Create_tmp_table_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Lock_tables_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Execute_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Repl_slave_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Repl_client_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Create_view_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Show_view_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Create_routine_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Alter_routine_priv'] == 'Y')
			echo "INSERT, ";
		if($permrow['Create_user_priv'] == 'Y')
			echo "INSERT, ";
		echo "permissions on ".$currentDb."<br>\n";
			echo "</div>";
			echo "</td></tr>";
		$placeholder = $placeholder + "10";
	
	}

}
?>
